#include <QDateTime>
#include <QString>
#include <QDebug>
#include <QByteArray>
#include <QTimer>
#include "comm_utils.h"

QString LogUtils::makeLogBin(const QString &message, const ev_packet_t *packet)
{
    QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    if (packet != nullptr) {
        QByteArray dataArray(reinterpret_cast<const char*>(packet->data), packet->len);
        QString hexData = dataArray.toHex(' ').toUpper();
        return QString("[%1] %2 (user=%3, seq=%4, cmd=0x%5, len=%6, data=[%7])")
            .arg(timestamp)
            .arg(message)
            .arg(packet->user)
            .arg(packet->seq)
            .arg(packet->cmd, 4, 16, QLatin1Char('0'))
            .arg(packet->len)
            .arg(hexData);
    }

    return QString("[%1] %2")
            .arg(timestamp)
            .arg(message);

}

QString LogUtils::makeLogStr(const QString &message, const ev_packet_t *packet)
{
    QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    if (packet != nullptr) {
        QString str = QString::fromUtf8((char *)packet->data, packet->len);
        return QString("[%1] %2 (user=%3, seq=%4, cmd=0x%5, len=%6, data=[%7])")
            .arg(timestamp)
            .arg(message)
            .arg(packet->user)
            .arg(packet->seq)
            .arg(packet->cmd, 4, 16, QLatin1Char('0'))
            .arg(packet->len)
            .arg(str);
    }

    return QString("[%1] %2")
            .arg(timestamp)
            .arg(message);

}
